package com.recruit.wechat.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recruit.wechat.bean.TreeDto;
import com.recruit.wechat.controller.entity.ApiCode;
import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import com.recruit.wechat.utils.TreeToolUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.recruit.wechat.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;

import com.recruit.wechat.bean.WechatIndustry;


import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LF
 * @since 2020-08-25
 */
@Controller
@RequestMapping("/WeChatUser/wechatIndustry")
public class WechatIndustryController extends BaseController {
    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main() {
        return "wechatIndustry/WechatindustryMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addWechatIndustryHtml")
    public String addWechatIndustryHtml() {
        return "wechatIndustry/WechatindustryAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editWechatIndustryHtml")
    public String editWechatIndustryHtml(Integer id, Model model) {
        WechatIndustry wechatIndustry = wechatIndustryService.selectWechatIndustryByIdWechatIndustry(id);
        model.addAttribute("wechatIndustry", wechatIndustry);
        //通过父级id查找父级名称
        WechatIndustry wechatIndustry1 = wechatIndustryService.getById(wechatIndustry.getParentId());
        model.addAttribute("parentName", wechatIndustry1!=null?wechatIndustry1.getIndustryName():"行业信息");
        return "wechatIndustry/WechatindustryEdit";
    }

    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showWechatIndustryHtml")
    public String showWechatIndustryHtml(Integer id, Model model) {
        WechatIndustry wechatIndustry = wechatIndustryService.selectWechatIndustryByIdWechatIndustry(id);
        model.addAttribute("wechatIndustry", wechatIndustry);
        //通过父级id查找父级名称
        WechatIndustry wechatIndustry1 = wechatIndustryService.getById(wechatIndustry.getParentId());
        model.addAttribute("parentName", wechatIndustry1!=null?wechatIndustry1.getIndustryName():"行业信息");
        return "wechatIndustry/WechatindustryShow";
    }


    /**
     * 分页查询WechatIndustry
     *
     * @param page           页码
     * @param limit          查几条
     * @param wechatIndustry 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findWechatIndustryByPage")
    @ResponseBody
    public ResultPage findWechatIndustryByPage(Integer page, Integer limit, WechatIndustry wechatIndustry) {
        return wechatIndustryService.findWechatIndustryByPage(page, limit, wechatIndustry);
    }

    /**
     * 添加WechatIndustry
     *
     * @param wechatIndustry 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addWechatIndustry")
    @ResponseBody
    public ApiResultModel saveWechatIndustry(WechatIndustry wechatIndustry) {
        return wechatIndustryService.addWechatIndustry(wechatIndustry);
    }

    /**
     * 编辑WechatIndustry
     *
     * @param wechatIndustry 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editWechatIndustry")
    @ResponseBody
    public ApiResultModel editWechatIndustry(WechatIndustry wechatIndustry) {
        return wechatIndustryService.editWechatIndustry(wechatIndustry);
    }


    /**
     * 删除一条WechatIndustry
     *
     * @param id 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delWechatIndustry")
    @ResponseBody
    public ApiResultModel delWechatIndustry(Integer id) {
        //通过ID查询所有子集
        List<WechatIndustry> wechatIndustryList = wechatIndustryService.findAllIndustryById(id);
        //删除所有子集
        for (WechatIndustry w:wechatIndustryList) {
            //System.out.println(w.toString());
            wechatIndustryService.delWechatIndustry(w.getIndustryId());
        }
        return $.success(ApiCode.success, ApiCode.success_msg, null);
    }


    /**
     * 获取父级 0为 的 大体行业信息
     *
     *
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/getAllIndustries")
    @ResponseBody
    public ApiResultModel getAllIndustries(String id) {


        QueryWrapper<WechatIndustry> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("parent_id", id);

        List<WechatIndustry> allIndustries = wechatIndustryService.list(queryWrapper);

        return $.success(ApiCode.success, ApiCode.success_msg, allIndustries);

    }

    @RequestMapping(value = "admin/getTree")
    @ResponseBody
    public ApiResultModel reLoadTree(){
        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点

        List<WechatIndustry> industryList = wechatIndustryService.list();


        for (int i = 0; i < industryList.size(); i++) {
            TreeDto treeDto = new TreeDto(
                    industryList.get(i).getIndustryId().toString(),
                    industryList.get(i).getIndustryName(),
                    industryList.get(i).getParentId().toString(),
                    "true",null
            );
            bodyList.add(treeDto);
        }




        TreeDto treeDto = new TreeDto("0", "行业信息", "null", "true",null);

        rootList.add(treeDto);

        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);


        List<TreeDto> result =  utils.getTree();

        return  $.success(ApiCode.success, ApiCode.success_msg, result);
    }


}
